package com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.scItem;

import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.SalesPlanStatAtomAbstract;
import com.alibaba.citrus.ots.sales.plan.facade.service.statistics.stat.model.SalesPlanStatRequest;
import com.alibaba.cz.base.tool.log.Log;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StopWatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author luyi
 * @date 2021/12/2 5:39 下午
 */
public class ScItemStatService extends SalesPlanStatAtomAbstract {

    private static final Log log = Log.getLogger(ScItemStatService.class);

    public ScItemStatService(SalesPlanStatRequest statRequest, CountDownLatch latch){
        super.statRequest = statRequest;
        super.latch = latch;
    }

    @Override
    protected void handle(SalesPlanStatRequest statRequest) {
        try {
            planScItemCollect(statRequest);
        } catch (InterruptedException e) {
            log.error("ScItemStatService.InterruptedException:",e);
        }
    }

    @Override
    protected String taskName() {
        return "scItemStatService.gather";
    }

    /**
     * 货品维度汇总
     */
    public void planScItemCollect(SalesPlanStatRequest statRequest) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        salesPlanTaskExecutor.execute(new ScItemEmployeeStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new ScItemDepartmentStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new ScItemChannelStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new ScItemOrganizationStatService(statRequest,countDownLatch));
        salesPlanTaskExecutor.execute(new ScItemBusinessUnitStatService(statRequest,countDownLatch));
        countDownLatch.await(20, TimeUnit.SECONDS);
    }

}
